package 题目集.倍增算法.lca;

import java.io.*;
import java.util.*;

/**
 * https://www.luogu.com.cn/problem/P3379
 * todo:没掌握tarjan算法的思想
 */
public class ch03_Tarjan {
    // 定义最大节点数和最大深度
    static int MAXN = 500010;
    static int MAXM = 20;
    // 定义父节点数组
    static int[] p = new int[MAXN];
    static boolean[] vis = new boolean[MAXN];
    // 定义图的邻接表表示树
    static List<Integer>[] graph = new ArrayList[MAXN];
    static int n, m, s;
    static Map<Integer, Integer>[] ask = new Map[MAXN];//存储节点对应的所有询问以及答案
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) {
        Reader sc = new Reader(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        s = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            graph[i] = new ArrayList<>();
            p[i] = i;
        }
        for (int i = 1; i <= m; i++) {
            ask[i] = new HashMap<>();
        }
        for (int i = 1; i < n; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            graph[u].add(v);
            graph[v].add(u);
        }
        int[][] list = new int[m][2];
        for (int[] issue : list) {
            issue[0] = sc.nextInt();
            issue[1] = sc.nextInt();
            ask[issue[0]].put(issue[1], null);
            ask[issue[1]].put(issue[0], null);
        }
        tarjan(s, s);
        for (int[] issue : list) {
            Integer ans1 = ask[issue[0]].get(issue[1]);
            Integer ans2 = ask[issue[1]].get(issue[0]);
            if (ans1 == null) {
                out.println(ans2);
            } else {
                out.println(ans1);
            }
        }
        out.flush();
    }

    public static void tarjan(int cur, int parent) {
        vis[cur] = true;
        for (Integer to : graph[cur]) {
            if (!vis[to]) tarjan(to, cur);
        }
        ask[cur].forEach((k, v) -> {
            if (vis[k]) {
                ask[k].put(cur, find(k));
            }
        });
        p[cur] = parent;
    }

    public static int find(int o) {
        return p[o] == o ? o : (p[o] = find(p[o]));
    }

    static class Reader {
        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public Reader(Object... o) {
        }

        public int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {
            }
            return (int) in.nval;
        }
    }
}
